################################################################################
# 01-figure_01_table.R
# Paper:    Bottom Up? Top Down? Determinants of Issue-Attention in State 
#           Politics.
# Journal:  Journal of Politics.
# Authors:  Andreu Casas, Oscar Stuhler, Julia Payson, Joshua A. Tucker, 
#           Richard Bonneau, and Jonathan Nagler.
# Purpose:  This script compiles the information for Table 4 — the average daily 
#           issue-attention by group in 2018 and 2021. Values were generated by 
#           first averaging the issue distributions of all tweets sent by a group 
#           on a given day and then averaging over all days. For political 
#           groups, both parties were weighted equally. Note that we also
#           generate LaTeX code which we used to compile our manuscript.
# Out File: ./tables/figure02.png
# Data In:  
#           1. the aggregated prevalences for 2018:  
#              ./data/avg-daily-att-per-issue-and-group-2018.csv
#           2. the aggregated prevalences for 2021
#              ./data/avg-daily-att-per-issue-and-group-2021.csv
#           
# Data Out:  
#           This script does not save any files, it prints the table to the 
#           console.
################################################################################


#===============================================================================
# PACKAGES
#===============================================================================
library(dplyr)
library(tidyr)
library(xtable)

#===============================================================================
# MAIN
#===============================================================================
# Load data and generate tables

# 2018
#-------------------------------------------------------------------------------
year <- "2018"
load_file <- paste0("./data/avg-daily-att-per-issue-and-group-", year, ".csv")
db <- read.csv(load_file)
db$Issue <- ifelse(db$Issue == "Domestic Commerce", "Domestic\nCommerce", db$Issue)
db$Group <- ifelse(db$Group == "Trump", "President", db$Group)
db$Group <- ifelse(db$Group == "Random public", "Random Public", db$Group)
db$Group <- recode(db$Group,
                  `Members of Congress` = "MoC_18",
                  `National Media` = "NM_18",
                  `State Legislators` = "SL_18",
                  `State Media` = "SM_18", 
                  `State Partisans` = "SP_18",
                  `Random Public` = "SGP_18", 
                  `President` = "P_18")
db$overall_mean <- NULL

# Transform the dataframe
df_wide <- db %>%
  pivot_wider(names_from = Group, values_from = Value)
df_wide$`Mean_18` <- rowMeans(df_wide[,2:ncol(df_wide)])
df_wide[,2:ncol(df_wide)] <- round(df_wide[,2:ncol(df_wide)], 1)
df_wide <- df_wide[order(-df_wide$Mean_18), ]
df_wide$SGP_18 <- "-"
df_wide <- df_wide[,c("Issue", "SL_18", "SM_18", "SP_18", "SGP_18",
                      "MoC_18", "NM_18", "P_18", "Mean_18")]


# 2021
#-------------------------------------------------------------------------------
year <- "2021"
load_file <-  paste0("./data/avg-daily-att-per-issue-and-group-", year, ".csv")
db <- read.csv(load_file)
db$Issue <- ifelse(db$Issue == "Domestic Commerce", "Domestic\nCommerce", db$Issue)

db$Group <- ifelse(db$Group == "Trump", "President", db$Group)
db$Group <- ifelse(db$Group == "Random public", "Random Public", db$Group)
db$Group <- recode(db$Group,
                  `Members of Congress` = "MoC_21",
                  `National Media` = "NM_21",
                  `State Legislators` = "SL_21",
                  `State Media` = "SM_21", 
                  `State Partisans` = "SP_21",
                  `Random Public` = "SGP_21", 
                  `President` = "P_21")
db$overall_mean <- NULL

# Transform the dataframe
df_wide_21 <- db %>%
  pivot_wider(names_from = Group, values_from = Value)
df_wide_21$Mean_21 <- rowMeans(df_wide_21[,2:ncol(df_wide_21)])
df_wide_21[,2:ncol(df_wide_21)] <- round(df_wide_21[,2:ncol(df_wide_21)], 1)
df_wide_21 <- df_wide_21[order(-df_wide_21$Mean_21), ]

df_wide_21 <- df_wide_21[,c("Issue", "SL_21", "SM_21", "SP_21", "SGP_21", 
                            "MoC_21", "NM_21", "P_21", "Mean_21")]

# COMBINE 2018 & 2021 into single table. 
# TABLE 4
#-------------------------------------------------------------------------------
# - combine them
outdb <- left_join(df_wide, df_wide_21)

# - sort the columns
outdb <- outdb %>%
  dplyr::select(Issue, SL_18, SL_21, SM_18, SM_21, SGP_18, SGP_21, SP_18, SP_21, 
                MoC_18, MoC_21, NM_18, NM_21, P_18, P_21, Mean_18, Mean_21)


#===============================================================================
# OUTPUT
#===============================================================================
# Table 2 of the paper
print(outdb %>% as.data.frame())
 #              Issue SL_18 SL_21 SM_18 SM_21 SGP_18 SGP_21 SP_18 SP_21 MoC_18 MoC_21 NM_18 NM_21 P_18 P_21 Mean_18 Mean_21
 #    Gov. Operations  25.0  22.7  20.3  11.6      -   27.0  33.4  25.1   19.8   16.3  22.8  16.3 38.5  7.1    26.2    18.0
 #       Civil Rights   8.1   9.2   9.8  10.0      -   17.0  13.3  15.2    6.8    8.5  10.9  12.7  1.6  7.0     9.8    11.4
 #      Intl. Affairs   2.3   1.9   5.7   5.4      -    6.2   7.6   6.3    5.3    5.9  21.0  19.2 15.3  4.9     9.1     7.1
 #        Immigration   3.7   2.4   4.9   2.5      -    3.9   7.1   4.5    6.5    6.1   6.6   4.2 14.7  1.6     7.0     3.6
 #            Economy   7.9   7.1   6.4   6.4      -    4.0   4.2   4.4    9.4    9.2   4.9   4.3  9.0 18.1     6.5     7.6
 #         Healthcare   8.6  16.3   6.6  19.9      -   10.3   4.2  11.4    9.5   13.6   4.2  14.7  1.5 22.3     5.8    15.5
 #      Law and Crime   8.1   6.6   7.7   5.4      -    5.6   4.9   5.7    6.7    5.1   5.7   5.8  2.4  2.6     5.8     5.3
 #        Gun Control   1.7   1.7   4.4   1.8      -    5.6   7.4   4.8    1.6    1.3   3.5   1.7  2.3  0.7     4.3     2.5
 #            Defense   3.6   2.8   1.7   1.6      -    2.8   2.8   2.8    7.2    6.5   3.9   3.3  2.9  5.2     3.5     3.6
 #          Education   7.3   6.2   5.6   6.0      -    2.4   2.5   3.4    3.1    2.9   1.6   2.1  0.2  2.2     3.4     3.6
 # Domestic\nCommerce   3.9   4.2   3.9   5.8      -    2.9   2.0   2.5    4.6    4.2   3.2   2.7  2.6  6.0     3.3     4.1
 #        Environment   2.9   3.0   3.9   4.0      -    2.8   2.3   3.3    3.4    3.9   2.4   3.1  0.3  3.9     2.6     3.4
 #      Foreign Trade   0.4   0.1   1.3   0.3      -    0.4   0.7   0.4    1.1    0.3   2.5   0.5  6.1  0.4     1.8     0.4
 #     Transportation   3.1   2.5   3.5   3.4      -    1.0   0.9   1.3    2.1    3.2   0.9   1.6  0.7  6.9     1.7     2.9
 #         Technology   1.5   1.2   1.2   1.3      -    1.8   1.5   1.6    2.0    1.8   1.7   2.1  0.2  1.2     1.6     1.6
 #             Energy   1.9   2.0   2.9   2.6      -    1.3   1.1   1.7    1.5    2.3   1.0   1.5  0.5  0.8     1.5     1.8
 #              Labor   2.9   2.5   1.7   2.2      -    1.3   1.1   1.6    2.6    3.1   0.9   1.5  0.1  6.2     1.5     2.6
 #            Housing   1.7   2.5   4.6   5.8      -    1.1   0.8   1.3    1.0    1.2   0.7   0.9  0.0  0.7     1.4     1.9
 #     Social Welfare   2.2   2.5   0.8   0.9      -    1.2   0.9   1.3    1.9    1.9   0.4   0.5  0.2  1.2     1.1     1.4
 #        Agriculture   1.6   1.1   0.9   0.9      -    0.5   0.5   0.6    2.4    1.2   0.6   0.4  0.7  0.1     1.0     0.7
 #       Public Lands   1.5   1.6   2.2   2.3      -    0.6   0.5   0.7    1.8    1.4   0.7   0.8  0.0  1.0     1.0     1.2